package com.easycar.common.manager;

import android.content.Context;
import android.os.Build;

import com.easycar.common.base.GRunTime;
import com.easycar.common.net.callback.FileCallBack;
import com.easycar.common.util.DeviceInfo;
import com.easycar.common.util.OkHttpUtil;
import com.easycar.common.util.PreferenceUtil;

import java.util.HashMap;
import java.util.Map;

/**
 *
 *  光享 网络封装类
 */

public class NetWorkManager {
    public  static  final  NetWorkManager  INSTANCE=new NetWorkManager();

    public  static  NetWorkManager getInstance(){

        return INSTANCE;
    }



    public  Map<String ,String> addHeader(Context context){
        Map<String, String> map = new HashMap<>();
        map.put("uuid", Build.SERIAL);
        map.put("phoneType", Build.MODEL);
        map.put("osver", Build.VERSION.RELEASE);
        map.put("platform", "ANDROID");
        map.put("appver", DeviceInfo.getAppVersion(context) );
        map.put("token", PreferenceUtil.getInstance(context).getString("sdk_token",""));
        return map;
    }
        private String getURL(String url){
            StringBuilder sbUrl=new StringBuilder();
            sbUrl.append(GRunTime.getInstance().getURL());
            sbUrl.append(url);
            return  sbUrl.toString();
        }




    public    <T> void   post(Context context, String url, Map<String,String> map, GCallBack<T> callBack){
           OkHttpUtil.post().url(getURL(url)).params(map).headers(addHeader(context)).build().execute(callBack);
       }
        public  <T>  void   get(Context context, String url, Map<String,String> map, GCallBack<T>  callBack){
             OkHttpUtil.get().url(getURL(url)).params(map).headers(addHeader(context)).build().execute(callBack);
        }

    /**
     * 下载文件
     * @param context
     * @param method 方法
     * @param map
     * @param callBack
     */
     public  void  downLoadFile(Context context,String method, Map<String,String> map, FileCallBack callBack) {
         OkHttpUtil.post().url(getURL(method)).params(map).headers(addHeader(context)).build().execute(callBack);
     }


}
